package a.b;

import basic.common.ws.Message;
import com.alibaba.fastjson.JSON;
import okhttp3.*;
import okio.ByteString;

import java.util.function.Function;

/*
 *  消息客户端
 *  <li>    使用okhttp
 */
public class MsgJavaClient {
    Function<String,Void> msgV1Function;
    private WebSocket ws;
    String host;

    int port;
    String token;

    String loginFrom;
    private OkHttpClient client_;

    public Function<String, Void> getMsgV1Function() {
        return msgV1Function;
    }

    public void setMsgV1Function(Function<String, Void> msgV1Function) {
        this.msgV1Function = msgV1Function;
    }

    public WebSocket getWs() {
        return ws;
    }

    public void setWs(WebSocket ws) {
        this.ws = ws;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public String getLoginFrom() {
        return loginFrom;
    }

    public void setLoginFrom(String loginFrom) {
        this.loginFrom = loginFrom;
    }

    public OkHttpClient getClient_() {
        return client_;
    }

    public void setClient_(OkHttpClient client_) {
        this.client_ = client_;
    }

    public MsgJavaClient(Function<String,Void> msgV1Function) {
        this.msgV1Function = msgV1Function;
    }

    public void start(){
        client_ = new OkHttpClient();
        StringBuilder sb = new StringBuilder();
        String s = sb.append("ws://")
                .append(host)
                .append(":")
                .append(port)
                .append("/ws/ksv.do?")
                .append("token=")
                .append(token)
                .append("&loginFrom=")
                .append(loginFrom)
                .toString();


        Request request = new Request.Builder()
                .url(s)
                .build();

        WebSocketListener listener = new WebSocketListener() {
            @Override
            public void onMessage(WebSocket webSocket, String text) {
                System.out.println(text);
            }

            @Override
            public void onOpen(WebSocket webSocket, Response response) {
                System.out.println("open");
            }

            @Override
            public void onMessage(WebSocket webSocket, ByteString bytes) {
                super.onMessage(webSocket, bytes);
            }

            @Override
            public void onClosing(WebSocket webSocket, int code, String reason) {
                super.onClosing(webSocket, code, reason);
                System.out.println("closeing");
            }

            @Override
            public void onClosed(WebSocket webSocket, int code, String reason) {
                super.onClosed(webSocket, code, reason);
                System.out.println("closed");
            }

            @Override
            public void onFailure(WebSocket webSocket, Throwable t,Response response) {
                super.onFailure(webSocket, t, response);
                System.out.println("failure ");
                t.printStackTrace();
            }
        };
        ws = client_.newWebSocket(request, listener);
    }

    public void shutdown(){
        ws.close(1000,"关闭ws");
    }

    public void sendMsg(Message msg){
        boolean send = ws.send(JSON.toJSONString(msg));
        System.out.println(send);
    }

}
